import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';

void main(List<String> args) async {
  File file = File(
      "/Users/lipenghui/Documents/flutter_project/finance_app/lib/l10n/intl_en.arb");
  final data = await file.readAsString();
  Map<String, dynamic> jsonMap = json.decode(data);
  Map<String, String> resultMap = {};
  for (final item in jsonMap.entries) {
    resultMap[item.key] = await transform(item.value.toString());
  }
  File resultFile = File(
      '/Users/lipenghui/Documents/flutter_project/finance_app/lib/l10n/intl_zh.arb');
  if (!(await resultFile.exists())) {
    await resultFile.create();
  }
  print(resultMap);
  await resultFile
      .writeAsString(JsonEncoder.withIndent("  ").convert(resultMap));
  print('done.');
  exit(0);
}

//翻译文件
Future<String> transform(String content) async {
  final result = await Dio().get(
      'http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i=$content');
  final resultString = result.data;
  print(resultString);
  StringBuffer buffer = StringBuffer();
  for(final item in resultString['translateResult']){
    buffer.write(item[0]['tgt']);
  }
  return buffer.toString();
}
